<%@page import="merlin.beans.*, merlin.managers.*,java.text.*,java.util.*" %>

<%!
	static final long MILLIS_IN_THREE_DAYS = 3 * 1000 * 60 * 60 * 24;
%>
<HTML>
	<HEAD>
	    <TITLE>Merlin Scott Management System</TITLE>

	    <!-- begin imports for creation of the tabs -->
		<link rel="stylesheet" type="text/css" href="mr_tabs/tabcontent.css" />
		<script type="text/javascript" src="mr_tabs/tabcontent.js">
		</script>
		<!-- ends imports for creation of the tabs -->

		<!-- begin DWR imports -->
		<script type='text/javascript' src='dwr/interface/MSALControlSystemManager.js'></script>
		<script type='text/javascript' src='dwr/engine.js'></script>
		<script type='text/javascript' src='dwr/util.js'></script>
		<!-- ends DWR imports -->

		<!-- begin Prototype import -->
		<script type='text/javascript' src='mr_scripts/prototype.js'></script>
		<!-- ends Prototype import -->

		<script type="text/javascript">

			function unsubscribeList(frm){
				var emailIds = frm.txtEmailIds.value;
				if(emailIds.length > 0){
					MSALControlSystemManager.unsubscribeList(emailIds,callbackUnsubscribeList);
				}else{
				}
			}

			function callbackUnsubscribeList(updateCount){
				document.frmMailinList.txtEmailIds.value = "";
				alert(updateCount + " records updated");
			}

			function logOut(frm){
				document.location = 'mr_loginControlSystem.jsp';
			}

			function generateMailingList(frm){
				var reportNumber=0;
				for(var i=0;i<frm.report.length;i++){
					if(frm.report[i].checked){
						reportNumber = parseInt(frm.report[i].value);
						break;
					}
				}

				var selectYoungest = (frm.directorAge[0].checked ? true : false);

				MSALControlSystemManager.generateMailingList(selectYoungest,reportNumber,handleGenerateList);
			}

			function handleGenerateList(){
				alert("mailing list generation has started");
				document.location.reload();
			}

			function cleanWebsiteAddresses(){
				MSALControlSystemManager.cleanWebsiteNamesAndEmailAddresses(handleCleanWebsiteAddressesAndEmailAddresses);
			}

			function handleCleanWebsiteAddressesAndEmailAddresses(result){
				alert(result[0] + " website addresses and " + result[1] + " emails updated");
			}

			function downloadEmailList(listingId){
				document.location = "merlin?mode=downloadMailingList&listingId=" + listingId;
			}

			function deleteEmailListings(frm){
				if(frm.listingId){
					var str="";
					if(!frm.listingId.length){
						// there is only one row in the table
						if(frm.listingId.checked){
							str += "," + frm.listingId.value;
						}
					}else{
						for(var i=0;i<frm.listingId.length;i++){
							if(frm.listingId[i].checked){
								str += "," + frm.listingId[i].value;
							}
						}
					}
					if(str.length > 0){
						str = str.substring(1);
						MSALControlSystemManager.deleteEmailListings(str,handleDeleteEmailListings);
					}else{
						alert("Nothing to delete");
					}
				}else{
					alert("No rows to delete");
				}
			}

			function handleDeleteEmailListings(result){
				alert("Deleted " + result + " records");
				document.location.reload();
			}
		</script>

	</HEAD>
	<BODY>
		<center>
	        <jsp:include page = "mr_headerSmall.jsp"/>
	        <p/>
			<!-- Main Tab Container -->
			<!--div id="mainTabContainer" style="height:505px;width:750px;">
				&nbsp;&nbsp;&nbsp;&nbsp;
				<ul id="maintab" class="shadetabs">
					<li class="selected"><a href="#" rel="divMailingList">MAILING LIST&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></li>
					<li><a href="#" rel="divResearcherUpdates">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RESEARCHER UPDATES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></li>
					<li><a href="#" rel="divIndustryAverages">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;INDUSTRY AVERAGES</a></li>
				</ul>
			</div-->

			<div id="divMailingList"  style="height:500px;width:750px;border-style:solid;border-width:2px;border-color:yellow;">
				<form name='frmMailinList'>
					<p>&nbsp;<br/>
					<table width='90%'style='font-size:11px'>
						<tr>
							<td width="45%">
								<textarea name="txtEmailIds" style="width:270px;height:100px"></textarea>
							</td>
							<td>
								Unsubscribe List
								<p>&nbsp;<br/>
								<input type="button" value="Update" onclick="unsubscribeList(this.form)">
								<p><input type="button" value="Clean Website Addresses" onclick="cleanWebsiteAddresses()">
							</td>
						</tr>
						<tr>
							<td>
								<br/>
								<table style='font-size:11px'>
									<tr>
										<td>
											<input type="radio" name="directorAge" value="youngest" checked>Youngest Male Director
											<br/>
											<input type="radio" name="directorAge">Oldest Male Director
										</td>
										<td>
											<input type="radio" name="report" value="1" checked>Report 1
											<br/>
											<input type="radio" name="report" value="2" >Report 2
											<br/>
											<input type="radio" name="report" value="3" >Report 3
											<br/>
											<input type="radio" name="report" value="4" >Report 4
										</td>
								</table>
							</td>
							<td>
								<br/>
								<p>&nbsp;<p/>
								<input type="button" value="Create List" onclick="generateMailingList(this.form)">
							</td>
						</tr>
						<tr>
							<td colspan='2'>
								<p>
								<input type="button" value="Delete Listings" onclick="deleteEmailListings(this.form)">
								<input type="button" value="Refresh List" onclick="document.location.reload()">
								<div style="height:200px;overflow: auto;border-style: solid;border-width: 1px;">
									<table  width='100%' cellspacing='0' cellpadding = '5' style="font:11px verdana,arial, sans-serif ;">
										<thead>
											<tr>
												<th width='5%'></th>
												<th>Mailing List Type</th>
												<th>Creation Date</th>
												<th>Status</th>
											</tr>
										</thead>
										<tbody>
										<%
											EmailListingBean[] beans = MSALControlSystemManager.getEmailListingBeans();
											SimpleDateFormat dfmt = new SimpleDateFormat("dd/MM/yy");

											Calendar today = Calendar.getInstance();
											today.set(Calendar.HOUR_OF_DAY,0);
											today.set(Calendar.MINUTE,0);
											today.set(Calendar.SECOND,0);

											long todaysTimeInMillis = today.getTimeInMillis();

											for(int i=0;i<beans.length;i++){
												// default behavior : list is being prepared.
												String status = "Preparing List";
												String rowColor = "black";
												boolean clickable = false;
												String cursor = "default";

												if(beans[i].isListingAvailable()){ // email list has been created
													if(beans[i].getDownloadDate() == null){ // but has never been downloaded.
														status = "Click to Download";
														rowColor = "#038243";
														clickable = true;
														cursor = "pointer";
													}else{ // has been downloaded before.
														if((todaysTimeInMillis - beans[i].getDownloadDate().getTime()) <  MILLIS_IN_THREE_DAYS){ // last download was less than 3 days before.
															status = "Click to Download";
															rowColor = "#038243";
															clickable = true;
															cursor = "pointer";
														}else{ // last download was more than 3 days before, so download is no longer available.
															status = "Downloaded on " + dfmt.format(beans[i].getDownloadDate());
															rowColor = "red";
														}
													}
												}

												out.print("<tr style='color:" + rowColor + ";cursor:" + cursor + "'"  + ">");
										%>
												<td><input type='checkbox' name='listingId' value='<%=beans[i].getListingId()%>'></td>
												<td <%=(clickable?"onclick='downloadEmailList(" + beans[i].getListingId() + ")'":"")%>><%=beans[i].getDescription()%></td>
												<td <%=(clickable?"onclick='downloadEmailList(" + beans[i].getListingId() + ")'":"")%>><%=dfmt.format(beans[i].getCreationDate())%></td>
												<td <%=(clickable?"onclick='downloadEmailList(" + beans[i].getListingId() + ")'":"")%>><%=status%></td>
											</tr>
										<%
											}
										%>
										</tbody>
									</table>
								</div>
							</td>
						</tr>
					</table>
				<form>
			</div>

			<div id="divResearcherUpdates" class="tabcontent" style="height:500px;width:745px;border-style:solid;border-width:2px;border-color:yellow">
			</div>

			<div id="divIndustryAverages" class="tabcontent" style="height:500px;width:745px;border-style:solid;border-width:2px;border-color:yellow">
			</div>
	   </center>

	</BODY>
</HTML>